package com.njlime.cms.controller.sys;

import com.njlime.cms.entities.MaterialsNews;
import com.njlime.cms.entities.MaterialsNewsGroup;
import com.njlime.cms.service.sys.MaterialImageGroupService;
import com.njlime.cms.service.sys.MaterialImageService;
import com.njlime.cms.service.sys.MaterialNewsGroupService;
import com.njlime.cms.service.sys.MaterialNewsService;
import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.TMemberGroup;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 素材管理-图文
 * Created by Dean on 2017/8/10.
 */
@Controller
@RequestMapping(value = "/cms/meterial")
public class MaterialNewsController extends BaseController {

    @Resource
    MaterialNewsService materialNewsService;

    @Resource
    MaterialNewsGroupService materialNewsGroupService;
    /**
     * 图文素材入口页面
     *
     * @return
     */
    @RequestMapping(value = "/toNewsList" )
    public ModelAndView roleManage(HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("/sys/content/meterial/news");


        return modelAndView;
    }

    /**
     * 选择图文素材入口页面
     *
     * @return
     */
    @RequestMapping(value = "/toChoseNewsList" )
    public ModelAndView toChoseNewsList(HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("/sys/content/meterial/chosenews");


        return modelAndView;
    }
    /**
     * 图文素材新增入口页面
     *
     * @return
     */
    @RequestMapping(value = "/toNewsAdd" )
    public ModelAndView toNewsAdd(HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("/sys/content/meterial/addNews");
      int unitId = UserUtils.getUser().getRootUnit().getId();
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(Integer.MAX_VALUE);
        String id = request.getParameter("id");
        if(id!=null) {
            modelAndView.addObject("info",  materialNewsService.get(Integer.parseInt(id), unitId));
        }
        modelAndView.addObject("groupList",materialNewsGroupService.list(unitId));
        return modelAndView;
    }
    /**
     * 图片新增
     * @param request
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNews")
    public String addNews(HttpServletRequest request,MaterialsNews info ){
        try {
           int unitId = UserUtils.getUser().getRootUnit().getId();

            info.setUnit_id(unitId);
			if(info.getId()!=null && info.getId()>0) {
				materialNewsService.update(info);
			}else{
	            info.setDel(0);


	            info.setCreate_time(new Date().getTime()/1000 );
	            materialNewsService.save(info);
			}
            return toJsonSuccessData();
        } catch (Exception e) {
            logger.error(e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 查询图片素材
     *
     * @param request
     * @param groupId 组id
     * @return
     */
    @RequestMapping(value = "getNewsList", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getNewsList(PageBean pageBean, HttpServletRequest request,int groupId,String name ) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();

            return toJsonListData(materialNewsService.getNewsList(pageBean ,unitId,groupId,name));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 删除图片素材
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "delNews", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delNews(String id, HttpServletRequest request ) {
        try {

           int unitId = UserUtils.getUser().getRootUnit().getId();

            materialNewsService.delNews( id,unitId);
            return toJsonSuccessData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 查询图片素材分组
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getNewsGroupList", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getNewsGroupList(PageBean pageBean, HttpServletRequest request ) {
        try {

            int unitId = UserUtils.getUser().getRootUnit().getId();

            Map map = materialNewsGroupService.getSumMember(unitId);
            List<MaterialsNewsGroup> list = materialNewsGroupService.list( unitId);
            Map resMap = new HashMap();

            resMap.put("sumMap",map);
            resMap.put("list",list);

            return toJsonListData(resMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 分组新增
     * @param request
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNewsGroup")
    public String addNewsGroup(HttpServletRequest request,MaterialsNewsGroup info ){
        try {
          int unitId = UserUtils.getUser().getRootUnit().getId();
            info.setId(null);
            info.setUnit_id(unitId);
            info.setCount(0);
            info.setDel(0);
            info.setCreate_time(new Date() );
            materialNewsGroupService.add(info);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 修改图片分组
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "editNewsGroup", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String editNewsGroup(String ids,int groupId, HttpServletRequest request, HttpServletResponse response) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();

            materialNewsService.editNewsGroup(ids,groupId,unitId);
            return toJsonSuccessData();
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }


    /**
     * 修改分组
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "editNewsGroupName", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String editNewsGroupName(String id,String name, HttpServletRequest request, HttpServletResponse response) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();

            materialNewsService.editGroup(id,name,unitId);
            return toJsonSuccessData();
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 删除分组
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "deleteNewsGroup", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteNewsGroup(String id,  HttpServletRequest request, HttpServletResponse response) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();

            materialNewsService.deleteGroup(id,unitId);
            return toJsonSuccessData();
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }
}
